//
//  OpenCVWrapper.m
//  Matting
//  Created by JoyWT on 2022/3/10
//  
    

#import <opencv2/opencv.hpp>
#import "OpenCVWrapper.h"
#include <opencv2/alphamat.hpp>
#import <opencv2/imgcodecs/ios.h>
@implementation OpenCVWrapper
+ (UIImage *)infoFlow:(UIImage *)orginImg trimap:(UIImage *)trimap{
    
    
    cv::Mat orginMat;
    cv::Mat orginMat2;

    UIImageToMat(orginImg,orginMat);
//
    cv::Mat trimapMat;
    cv::Mat trimapMat2;
//    cv::COLOR_BGR2GRAY
    UIImageToMat(trimap,trimapMat);
    cv::Mat outMat;
    std::cout<<orginMat.size<<std::endl;
    std::cout<<trimapMat.size<<std::endl;
    cv::cvtColor(orginMat, orginMat2,4); //deeplab生成的分类图转换的
//    cv::cvtColor(trimapMat, trimapMat2,7); // ps里面的透明图用这个方法
    
    cv::alphamat::infoFlow(orginMat2,trimapMat,outMat);
    
    
    UIImage *alphaImg= MatToUIImage(outMat);
    
    return alphaImg ;
}

+ (UIImage *)infoFlow3:(UIImage *)orginImg trimap:(UIImage *)trimap{


    cv::Mat orginMat;
//    UIImageToMat([UIImage imageNamed:@"plant.jpg"],orginMat);
//
    cv::Mat trimapMat;
//    UIImageToMat([UIImage imageNamed:@"plant.png"],trimapMat);

    cv::Mat outMat;
//    NSString *newPath=[NSString stringWithFormat:@"%@%@%@",[[NSBundle mainBundle]resourcePath],@"/",@"in.jpg"];
    NSString* filepath = [[NSBundle mainBundle] pathForResource:@"plant.jpg" ofType:nil];
    NSString* filepath2 = [[NSBundle mainBundle] pathForResource:@"plant.png" ofType:nil];

    const char* destDir = [filepath UTF8String];
    const char* destDir2 = [filepath2 UTF8String];

    orginMat =  cv::imread(destDir, 1);
//    trimapMat =  cv::imread(destDir2, 0);

//    NSLog(@"%@",orginMat.size);
//    NSLog(@"%@",trimapMat.size());
    
    std::cout<<orginMat.size<<std::endl;
    std::cout<<trimapMat.size<<std::endl;
    
    cv::alphamat::infoFlow(orginMat,trimapMat,outMat);




    UIImage *alphaImg= MatToUIImage(outMat);

    return alphaImg ;
}
bool matEqual(const cv::Mat& a, const cv::Mat& b) {
    cv::Mat temp;
    cv::bitwise_xor(a,b,temp);   //It vectorizes well with SSE/NEON
    return !(cv::countNonZero(temp) );
}

@end
